# -*- coding: utf-8 -*-
from email.policy import default
from framework.exts import db
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash

"""
以下特殊字段含义
    address_name:当前pusher接口的作用
    webhook: 推送钩子
    secret: 秘钥
"""

# 请求管理
class Pusher(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    pusher_name = db.Column(db.String(100))
    pusher_secret = db.Column(db.String(255))
    pusher_note = db.Column(db.String(255))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    create_time = db.Column(db.DateTime, index=True, default=datetime.now)
    update_time = db.Column(db.DateTime, index=True, default=datetime.now)


# 请求管理
class PusherWebhook(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    pusher_id = db.Column(db.Integer, db.ForeignKey('pusher.id'))
    webhook = db.Column(db.String(255))
    address_name = db.Column(db.String(100))
    secret = db.Column(db.String(100))
    pusher_type = db.Column(db.String(10), default="DingTalk")
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    create_time = db.Column(db.DateTime, index=True, default=datetime.now)
    update_time = db.Column(db.DateTime, index=True, default=datetime.now)


"""
以下特殊字段含义
    address_name: 当前pusher接口的作用
    pusher_id: 关联pusher_id
    text: 推送的内容
    msg_type: 消息类型(
        1: 支持Text消息
        2: 支持Link消息
        3: 支持image表情消息
        4: 支持Markdown消息
        5: 支持ActionCard消息
    )
"""
class Log(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    pusher_id = db.Column(db.Integer, db.ForeignKey('pusher.id'))
    pusher_webhook_id = db.Column(db.Integer)
    address_name = db.Column(db.String(100))
    text = db.Column(db.Text)
    data = db.Column(db.Text)
    msg_type = db.Column(db.Integer, default=1)
    error_msg = db.Column(db.String(100))
    status = db.Column(db.String(4), default="20")
    create_time = db.Column(db.DateTime, index=True, default=datetime.now)

    
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    create_time = db.Column(db.DateTime, index=True, default=datetime.now)
    update_time = db.Column(db.DateTime, index=True, default=datetime.now)

    def __repr__(self):
        return '<User {}>'.format(self.username)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)


